#!/bin/bash

# MQTT Multithread Client test

name="Multithread Client"
prog="examples/multithread/multithread"
no_pid=-1
broker_pid=$no_pid

source scripts/test_common.sh

# Use minimum of 2 seconds
# The check timout will sometimes incorrectly trigger if 1 second is used
def_args="-T -C 2000"

# Check for mosquitto
if command -v mosquitto
then
    bwrap_path="$(command -v bwrap)"
    if [ -n "$bwrap_path" ]; then
        # bwrap only if using a local mosquitto instance
        if [ "${AM_BWRAPPED-}" != "yes" ]; then
            echo "Using bwrap"
            export AM_BWRAPPED=yes
            exec "$bwrap_path" --unshare-net --dev-bind / / "$0" "$@"
        fi
        unset AM_BWRAPPED

        broker_args="-c scripts/broker_test/mosquitto.conf"
        port=11883
    else
        # mosquitto broker custom port non-TLS only
        has_tls=no
        generate_port
        broker_args="-p $port"
    fi
    mosquitto $broker_args &
    broker_pid=$!
    echo "Broker PID is $broker_pid"
    sleep 0.1

    def_args="${def_args} -h localhost"
    tls_port_args="-p 18883"
    port_args="-p ${port}"
    cacert_args="-A scripts/broker_test/ca-cert.pem"
fi

echo -e "Base args: $def_args $port_args"

# Run without TLS and QoS 0-2
./$prog $def_args $port_args -q 0 $1
RESULT=$?
[ $RESULT -ne 0 ] && echo -e "\n\n$name failed! TLS=Off, QoS=0" && do_cleanup "-1"

./$prog $def_args $port_args -q 1 $1
RESULT=$?
[ $RESULT -ne 0 ] && echo -e "\n\n$name failed! TLS=Off, QoS=1" && do_cleanup "-1"

./$prog $def_args $port_args -q 2 $1
RESULT=$?
[ $RESULT -ne 0 ] && echo -e "\n\n$name failed! TLS=Off, QoS=2" && do_cleanup "-1"

if test $has_tls == yes
then
    # Run with TLS and QoS 0-2
    ./$prog $def_args $cacert_args $tls_port_args -t -q 0 $1
    RESULT=$?
    [ $RESULT -ne 0 ] && echo -e "\n\n$name failed! TLS=On, QoS=0" && do_cleanup "-1"

    ./$prog $def_args $cacert_args $tls_port_args -t -q 1 $1
    RESULT=$?
    [ $RESULT -ne 0 ] && echo -e "\n\n$name failed! TLS=On, QoS=1" && do_cleanup "-1"

    ./$prog $def_args $cacert_args $tls_port_args -t -q 2 $1
    RESULT=$?
    [ $RESULT -ne 0 ] && echo -e "\n\n$name failed! TLS=On, QoS=2" && do_cleanup "-1"
fi

# End broker
do_cleanup "0"

echo -e "\n\nMultithread MQTT Client Tests Passed"

exit 0
